<?php

/*
 * /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
* php_Util_DB library
*
* This library provides some methods that provides extra facilities to php DB management
*
* - php_Util_DB (contains static methods)
* - php_Util_DB_CONSTANTS (contains constants)
* - php_Util_DB_CONNECTION (PHP Util DB Connection data)
* - php_Util_DB_EDITOR (Provides basic PHP database CRUD facilities)
*
*
* since 2013-02-21
* version 0.03b
*
*
*
* author: Felipe (felipe.camacho@gmail.com)
*  
*
* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
*/


namespace php_Util\DB;

/**
 * Namespace for Database library
 *
 */
use php_Util as ns_php_Util;

/**
 * Provides php database utilities
 * @author Felipe
 * @version 0.03b
 */
class php_Util_DB {

	
	/**
	 * Construct DNS from parameters and returns it.
	 * @param string $engine
	 * @param string $host
	 * @param string $database
	 * @return string
	 */
	public static function get_DNS($engine, $host, $database) {
		$dns = $engine.':dbname='.$database.";host=".$host;
		return $dns;
	}

}


/**
 * PHP Util DB constants
 *
 */
class php_Util_DB_CONSTANTS {

	const DB_CONNECTION_TYPE__PDO = "PDO";
	const DB_CONNECTION_TYPE__MySQLi = "MySQLi";
	
}


/**
 * PHP Util DB Connection data
 *
 */
class php_Util_DB_CONNECTION {

	public $type;
	public $hostName;
	public $userName;
	public $password;
	public $default_DB;
	public $options;

	/**
	 *  Connection link
	 */
	public $_connection_Link;

}



/**
 * Provides basic PHP database CRUD facilities.
 * @author Felipe
 *
 */
class php_Util_DB_EDITOR {

	
	/**
	 * 
	 * @var php_Util_DB_CONNECTION
	 */
	protected $db_Connection = null;
	

	public function get_DB_Connection() {
		return $this->db_Connection;
	}
	
	
	/**
	 * Connects to DB is there is no connection alive.
	 * @throws Exception
	 */
	public function connect_To_DB($connection = null) {

	}

	
	/**
	 * Create object
	 * @param string $tablename
	 * @param array $values
	 * 
	 * @throws Exception
	 * 
	 * @return int Affected rows
	 */
	public function create_Object($tablename, $values) {

	}

	
	/**
	 * Read object
	 * @param string $tablename
	 * @param array $conditions
	 * @param array $fields (optional)
	 * 
	 * @throws Exception
	 * 
	 * @return array
	 */
	public function read_Object($tablename, $conditions, $fields) {

	}
	
	
	/**
	 * Update object
	 * @param string $tablename
	 * @param array $updateFields
	 * @param string $conditions
	 *
	 * @throws Exception
	 *  
	 * @return int Affected rows
	 */
	public function update_Object($tablename, $updateFields, $conditions = null) {
		
	}
	
	
	/**
	 * Delete object
	 * @param string $tablename
	 * @param array||string $conditions
	 * 
	 * @throws Exception
	 * 
	 * @return int Affected rows
	 * 
	 */
	public function delete_Object($tablename, $conditions) {
	
	}
	
	
	/**
	 * Returns SQL query for insert
	 * @param string $tablename
	 * @param array $insertFields
	 * @throws Exception
	 * @return string
	 */
	public static function get_SQL__Insert($tablename, $insertFields) {
		

		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}
		
		
		if (!is_array($insertFields) || sizeof($insertFields) == 0) {
			throw new \Exception("Parameter insertFields required.");
		}
		// --------------------------------------------------------------------|/\|---
		
		
		// Prepare fields and values part of SQL query -----------------------------------------|\/|---
		$query_fields = '(';
		$query_values = '(';
		
		foreach ($insertFields as $insert_Field_key => $insert_Field_value) {
				
			if ($query_fields != '(') {
				$query_fields .= ", ";
			}
			$query_fields .=  "`$insert_Field_key`";
				
			if ($query_values != '(') {
				$query_values .= ", ";
			}
			$query_values .= "'$insert_Field_value'";
		}
		
		$query_fields .= ')';
		$query_values .= ')';
		
		// -------------------------------------------------------------------------------------|/\|---
		
		
		// Render SQL query ---------------------------------------------------------|-|---
		$sql_Query = "INSERT INTO `$tablename` $query_fields VALUES $query_values";
		

		return $sql_Query;
		
	}
	
	
	/**
	 * Returns SQL query for select
	 * @param string $tablename
	 * @param array $selectFields
	 * @param string|array $conditions (optional)
	 * @param string $order (optional)
	 * @throws Exception
	 * @return string
	 */
	public static function get_SQL__Select($tablename, $selectFields, $conditions = null, $order = null) {
		

		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}
		
		
		if ( ( $selectFields == null ) ||
			(!is_array($selectFields) && $selectFields == '') || 
			(is_array($selectFields) && sizeof($selectFields) < 1) ) {
			throw new \Exception("Parameter selectFields required. ['\$selectFields']". print_r($selectFields, true));
		}
		// --------------------------------------------------------------------|/\|---
		
		
		// Prepare select part of SQL query -----------------------------------------|\/|---
		$query_select = null;
		
		if (is_array($selectFields) && sizeof($selectFields) > 0) {
				
			$query_select = '';
			foreach ($selectFields as $select_value) {
					
				if ($query_select != '') {
					$query_select .= ", ";
				}
// 				$query_select .= $select_value;
				$query_select .=  "`$tablename`.`$select_value`";
			}
		
		} elseif ($selectFields != null && !is_array($selectFields)) {
			$query_select = $selectFields;
		}
		// --------------------------------------------------------------------------|/\|---
		
		
		// Prepare conditions part of SQL query -------------------------------------|\/|---
		$query_conditions = null;
		if (is_array($conditions) && sizeof($conditions) > 0) {
		
			$query_conditions = '';
				
			foreach ($conditions as $condition_key => $condition_value) {
		
				if ($query_conditions != '') {
					$query_conditions .= " AND ";
				}
				$query_conditions .= "`$tablename`.`$condition_key` = '$condition_value'";
			}
		
		} elseif ($conditions != null) {
			$query_conditions = $conditions;
		}
		// --------------------------------------------------------------------------|/\|---
		

		// Prepare SQL query ---------------------------------------|\/|---
		$sql_Query = "SELECT $query_select FROM `$tablename`";
		
		
		if ($query_conditions != null){
			$sql_Query .= " WHERE $query_conditions";
		}
		
		
		if ($order != null) {
			$sql_Query .= " ORDER BY $order";
		}
		// ---------------------------------------------------------|/\|---
		
		
		return $sql_Query;
		
	}
	
	
	/**
	 * Returns SQL query for update
	 * @param string $tablename
	 * @param array $updateFields
	 * @param string|array $conditions (optional)
	 * @throws Exception
	 * @return string
	 */
	public static function get_SQL__Update($tablename, $updateFields, $conditions = null) {
		

		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}
		
		
		if (!is_array($updateFields) || sizeof($updateFields) == 0) {
			throw new \Exception("Parameter updateFields required.");
		}
		// --------------------------------------------------------------------|/\|---
		
		
		// Prepare fields and values part of SQL query ----------------------------------------|\/|---
		$query_update_Fields = '';
		
		foreach ($updateFields as $update_Field_key => $update_Field_value) {
				
			if ($query_update_Fields != '') {
				$query_update_Fields .= ', ';
			}
				
			$query_update_Fields .= "`$update_Field_key` = '$update_Field_value'";
		}
		// ------------------------------------------------------------------------------------|/\|---
		
		
		// Prepare conditions part of SQL query -----------------------------------------------|\/|---
		$query_conditions = null;
		if (is_array($conditions) && sizeof($conditions) > 0) {
				
			$query_conditions = '';
				
			foreach ($conditions as $condition_key => $condition_value) {
		
				if ($query_conditions != '') {
					$query_conditions .= ' AND ';
				}
		
				$query_conditions .= "`$condition_key` = '$condition_value'";
			}

				
		} elseif ($conditions != null) {
			$query_conditions = $conditions;
		}
		// ------------------------------------------------------------------------------------|/\|---
		
		
		// Build SQL query ---------------------------------------|\/|---
		$sql_Query = "UPDATE $tablename SET $query_update_Fields";
		if ($query_conditions) {
			$sql_Query .= " WHERE $query_conditions";
		}
		// -------------------------------------------------------|/\|---
		
		return $sql_Query;
		
	}

	

	/**
	 * Returns SQL query for delete
	 * @param string $tablename
	 * @param string|array $conditions
	 * @throws Exception
	 * @return string
	 */
	public static function get_SQL__Delete($tablename, $conditions) {
	
		
		// Check required parameters ------------------------------------------|\/|---
		if ($tablename == null || $tablename == "") {
			throw new \Exception("Parameter tablename required.");
		}
		
		
		if (!is_array($conditions) || sizeof($conditions) == 0) {
			throw new \Exception("Parameter conditions required.");
		}
		// --------------------------------------------------------------------|/\|---
		
		
		// Prepare conditions part of SQL query -----------------------------------------------|\/|---
		$query_conditions = null;
		if (is_array($conditions) && sizeof($conditions) > 0) {
		
			$query_conditions = '';
		
			foreach ($conditions as $condition_key => $condition_value) {
		
				if ($query_conditions != '') {
					$query_conditions .= ' AND ';
				}
		
				$query_conditions .= "`$condition_key` = '$condition_value'";
			}
		
		
		
		} elseif ($conditions != null) {
			$query_conditions = $conditions;
		}
		// ------------------------------------------------------------------------------------|/\|---
		
		
		// Build SQL query ---------------------------------------|\/|---
		$sql_Query = "DELETE FROM `$tablename`";
		if ($query_conditions) {
			$sql_Query .= " WHERE $query_conditions";
		}
		// Build SQL query ---------------------------------------|/\|---
		
		
		return $sql_Query;
			
	}
	
}



/**
 * Provides facilities for php Util DB 
 * @author Felipe
 *
 */
class php_Util_DB_FACTORY {

	
	public static function get_NEW__php_Util_DB_Connection() {
		return new php_Util_DB_CONNECTION();
	}
	
	
	/**
	 * Returns new php_Util_DB_EDITOR
	 * @param string $editor_Type
	 * @return \php_Util\DB\php_Util_DB_EDITOR
	 */
	public static function get_NEW__php_Util_DB_Editor($editor_Type = NULL) {
		
		
		$php_Util_DB_Editor = null;
		
		// Check editor type is optional ---------------------------------------------|\/|---
		switch ($editor_Type) {
			
			case php_Util_DB_CONSTANTS::DB_CONNECTION_TYPE__PDO:
				$php_Util_DB_Editor = new php_Util_DB_EDITOR_PDO();
				break;

				
			default:
				$php_Util_DB_Editor = new php_Util_DB_EDITOR();
				break;
		}
		// ---------------------------------------------------------------------------|/\|---
		
		
		return $php_Util_DB_Editor;
		
	}
	
}

